﻿var globalTaskSystemInfoGuid = "e32cc6ac-199c-4a6f-93f8-937d3e482d13";          //  系统数据源标识
var globalTaskModuleInfoGuid = "62e86e8b-238b-4189-bc21-83696b7e32cc";          //  模块管理数据源标识
var globalTaskOrganizationInfoGuid = "0690f841-7773-445c-9f48-a7a0427686d5";    //  组织结构管理数据源标识
var globalTaskDataSouceInfoGuid = "06dc1c19-0121-4428-8e6e-a4818ec21a62";       //  数据源和界面管理数据源标识
var globalTaskAdapterInfoGuid = "08c32fc7-2664-4571-a7db-21486c085b6d";         //  适配器管理数据源标识

var globalTaskSiteInfoGuid = "6e042152-9277-496e-9f7d-a6fbe2f82daf";            //  网站配置信息数据源标识

var globalGuidListGuid = "c61ed474-e8b1-4fb4-8975-74b356caa22b";                //  检索列表数据的数据标识
var globalInitInfoGuid = "394672f4-3e9b-42ee-be2e-c1c4cde0996d";                //  初始化信息任务标识，用于提供环境变量初始化


function DataService() {
    this.url = "";
    this.SetTaskDataUrl = "";
    this.GetTaskDataUrl = "";
    this.TransformDataUrl = "";

    this.UserName = "";
    this.Password = "";
    this.CheckCode = "";
    this.UserID = "";

    this.xmlResult = document.createElement("xml");

    this.xmlSetData = document.createElement("xml");
    this.xmlSetData.loadXML("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                  "  <soap:Body>" +
                  "  <SetData xmlns='http://www.szjt.gov.cn/'>" +
                  "  <UserID/>" + 
                  "  <TaskGuid/>" +
                  "  <DataGuid/>" +
                  "  <DataType/>" +
                  "  <XmlData/>" +
                  "  </SetData>" +
                  "</soap:Body>" +
                  "</soap:Envelope>");
    this.xmlSetDataParam = this.xmlSetData.documentElement;

    this.xmlGetData = document.createElement("xml");
    this.xmlGetData.loadXML("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                  "  <soap:Body>" +
                  "  <GetData xmlns='http://www.szjt.gov.cn/'>" +
                  "  <UserID/>" +
                  "  <TaskGuid/>" +
                  "  <DataGuid/>" +
                  "  <DataType/>" +
                  "  </GetData>" +
                  "</soap:Body>" +
                  "</soap:Envelope>");
    this.xmlGetDataParam = this.xmlGetData.documentElement;

    this.xmlTransformData = document.createElement("xml");
    this.xmlTransformData.loadXML("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                  "  <soap:Body>" +
                  "  <TransformData xmlns='http://www.szjt.gov.cn/'>" +
                  "  <UserID/>" +
                  "  <TaskGuid/>" +
                  "  <XmlTransform/>" +
                  "  </TransformData>" +
                  "</soap:Body>" +
                  "</soap:Envelope>");
    this.xmlTransformDataParam = this.xmlTransformData.documentElement;

    this.xmlLoginTicket = document.createElement("xml");
    this.xmlLoginTicket.loadXML("<Document TaskGuid='0690f841-7773-445c-9f48-a7a0427686d5' DataGuid='' DataType='PageTicket'>" +
                  "  <UserNameTicket>" +
                  "    <UserName Type='TEXT'/>" +
                  "    <Password Type='TEXT'/>" +
                  "    <VerifyCode Type='TEXT'/>" +
                  "  </UserNameTicket>" +
                  "</Document>");
    this.xmlLoginTicketParam = this.xmlLoginTicket.documentElement;

};

DataService.prototype.LoginUser = function (url, userName, password, checkCode) {
    this.url = url;
    this.SetTaskDataUrl = url + "/SetData";
    this.GetTaskDataUrl = url + "/GetData";
    this.TransformDataUrl = url + "/TransformData";

    this.UserName = userName;
    this.Password = password;
    this.CheckCode = checkCode;
    this.UserID = "";

    return this.ReLoginUser();
};

DataService.prototype.LoginUserInfo = function (url, userID) {
    this.url = url;
    this.SetTaskDataUrl = url + "/SetData";
    this.GetTaskDataUrl = url + "/GetData";
    this.TransformDataUrl = url + "/TransformData";

    this.UserName = null;
    this.Password = null;
    this.UserID = userID;
};

DataService.prototype.ReLoginUser = function () {
    if (this.UserName == null)
        return;

    //<Document TaskGuid="0690f841-7773-445c-9f48-a7a0427686d5" DataGuid="" DataType="PageTicket">
    //    <UserNameTicket>
    //        <UserName Type="TEXT">账户名</UserName>
    //        <Password Type="TEXT">口令</Password>
    //        <VerifyCode Type="TEXT">验证码</VerifyCode >
    //    </UserNameTicket>
    //</Document>
    //返回值格式如下：
    //<Document TaskGuid="0690f841-7773-445c-9f48-a7a0427686d5" DataGuid="" DataType="PageTicket">
    //      <UserID>用户标识</UserID>
    //</Document>
    XmlNodeManage.SetSubValue(this.xmlLoginTicketParam, "//UserName", null, this.UserName);
    XmlNodeManage.SetSubValue(this.xmlLoginTicketParam, "//Password", null, this.Password);
    XmlNodeManage.SetSubValue(this.xmlLoginTicketParam, "//VerifyCode", null, this.CheckCode);
    var rValue = this.TransformDataSoap(globalTaskOrganizationInfoGuid, this.xmlLoginTicketParam);

    if (rValue != null && rValue.text != "") {
        this.UserID = rValue.text;
        return true;
    }

    return false;
};

DataService.prototype.GetNewGuid = function () {
    var retNode = this.GetDataSoap(globalTaskSystemInfoGuid, globalInitInfoGuid, "NewGuid");
    if (retNode != null)
        return retNode.text;
    else
        return "";
}

DataService.prototype.SetData = function (taskGuid, dataGuid, dataType, xmlData) {
    var result = null;

    if (xmlData == null) {
        xmlData = XmlNodeManage.GetDocumentNode(taskGuid, dataGuid, dataType);
    }
    $.ajax({
        type: "POST",
        dataType: "xml",
        url: this.SetTaskDataUrl,
        data: { UserID: this.UserID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType, XmlData: xmlData.xml },
        async: false,
        success: function (request, textStatus) {
            result = request.text;
        },
        error: function (request, textStatus, errorThrown) {
            alert(request.statusText);
            result = -1;
        }
    });
    return parseInt(result);
};

DataService.prototype.SetUserData = function (userID, taskGuid, dataGuid, dataType, xmlData) {
    var result = null;

    if (xmlData == null) {
        xmlData = XmlNodeManage.GetDocumentNode(taskGuid, dataGuid, dataType);
    }
    $.ajax({
        type: "POST",
        dataType: "xml",
        url: this.SetTaskDataUrl,
        data: { UserID: userID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType, XmlData: xmlData.xml },
        async: false,
        success: function (request, textStatus) {
            result = request.text;
        },
        error: function (request, textStatus, errorThrown) {
            alert(request.statusText);
            result = -1;
        }
    });
    return parseInt(result);
};

DataService.prototype.SetDataSoap = function (taskGuid, dataGuid, dataType, xmlData) {
    var result = null;
    XmlNodeManage.SetSubValue(this.xmlSetDataParam, "//UserID", null, this.UserID);
    XmlNodeManage.SetSubValue(this.xmlSetDataParam, "//TaskGuid", null, taskGuid);
    XmlNodeManage.SetSubValue(this.xmlSetDataParam, "//DataGuid", null, dataGuid);
    XmlNodeManage.SetSubValue(this.xmlSetDataParam, "//DataType", null, dataType);
    XmlNodeManage.SetSubValue(this.xmlSetDataParam, "//XmlData", null, xmlData.xml);

    $.ajax({
        type: "POST",
        dataType: "xml",
        contentType: "text/xml",
        url: this.url,
        data: this.xmlSetDataParam.xml,
        async: false,
        success: function (request, textStatus) {
            result = request.text;
        },
        error: function (request, textStatus, errorThrown) {
            alert(request.statusText);
            result = -1;
        }
    });
    return parseInt(result);
};

DataService.prototype.GetData = function (taskGuid, dataGuid, dataType) {
    var resultNode = null;
    var xResult = this.xmlResult;

    $.ajax({
        type: "GET",
        dataType: "xml",
        url: this.GetTaskDataUrl,
        data: { UserID: this.UserID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType },
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};

DataService.prototype.GetUserData = function (userID, taskGuid, dataGuid, dataType) {
    var resultNode = null;
    var xResult = this.xmlResult;

    $.ajax({
        type: "GET",
        dataType: "xml",
        url: this.GetTaskDataUrl,
        data: { UserID: userID, TaskGuid: taskGuid, DataGuid: dataGuid, DataType: dataType },
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};

DataService.prototype.GetDataSoap = function (taskGuid, dataGuid, dataType) {
    var resultNode = null;
    var xResult = this.xmlResult;

    XmlNodeManage.SetSubValue(this.xmlGetDataParam, "//UserID", null, this.UserID);
    XmlNodeManage.SetSubValue(this.xmlGetDataParam, "//TaskGuid", null, taskGuid);
    XmlNodeManage.SetSubValue(this.xmlGetDataParam, "//DataGuid", null, dataGuid);
    XmlNodeManage.SetSubValue(this.xmlGetDataParam, "//DataType", null, dataType);

    $.ajax({
        type: "POST",
        dataType: "xml",
        contentType: "text/xml",
        url: this.url,
        data: this.xmlGetDataParam.xml,
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};

DataService.prototype.TransformData = function (taskGuid, xmlParam) {
    var resultNode = null;
    var xResult = this.xmlResult;

    $.ajax({
        type: "POST",
        dataType: "xml",
        url: this.TransformDataUrl,
        data: { UserID: this.UserID, TaskGuid: taskGuid, XmlTransform: xmlParam.xml },
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};

DataService.prototype.TransformUserData = function (userID, taskGuid, xmlParam) {
    var resultNode = null;
    var xResult = this.xmlResult;

    $.ajax({
        type: "POST",
        dataType: "xml",
        url: this.TransformDataUrl,
        data: { UserID: userID, TaskGuid: taskGuid, XmlTransform: xmlParam.xml },
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};

DataService.prototype.TransformDataSoap = function (taskGuid, xmlParam) {
    var resultNode = null;
    var xResult = this.xmlResult;

    XmlNodeManage.SetSubValue(this.xmlTransformDataParam, "//UserID", null, this.UserID);
    XmlNodeManage.SetSubValue(this.xmlTransformDataParam, "//TaskGuid", null, taskGuid);
    XmlNodeManage.SetSubValue(this.xmlTransformDataParam, "//XmlTransform", null, xmlParam.xml);

    $.ajax({
        type: "POST",
        dataType: "xml",
        contentType: "text/xml",
        url: this.url,
        data: this.xmlTransformDataParam.xml,
        async: false,
        success: function (request, textStatus) {
            xResult.loadXML(request.text);
            resultNode = xResult.documentElement;
        },
        error: function (request, textStatus) {
            alert(request.statusText);
        }
    });
    return resultNode;
};
